package com.yonder.vfstest;

import java.io.File;
import java.io.RandomAccessFile;

import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;

/**
 * listen the change event of the application jar files
 * 
 * @author ijavaboy
 * @site <url>http://www.ijavaboy.com</url> 2013-5-10
 */
public class JarFileChangeListener implements FileListener {

	public JarFileChangeListener() {
	}

	@Override
	public void fileChanged(FileChangeEvent event) throws Exception {
		System.out.println("fileChanged" + event.getFile().getName());
	}

	@Override
	public void fileCreated(FileChangeEvent event) throws Exception {
		System.out.println("fileCreated" + event.getFile().getName());
	}

	@Override
	public void fileDeleted(FileChangeEvent event) throws Exception {
		System.out.println("fileDeleted" + event.getFile().getName());
	}

	
	public static void main(String[] args) throws Exception {
		FileSystemManager fileManager = VFS.getManager();
		File file = new File("test");
		FileObject monitoredDir = fileManager.resolveFile(file.getAbsolutePath());
		FileListener fileMonitorListener = new JarFileChangeListener();
		DefaultFileMonitor fileMonitor = new DefaultFileMonitor(fileMonitorListener);
		fileMonitor.setRecursive(true);
//		fileMonitor.setDelay(1000);
		fileMonitor.addFile(monitoredDir);
		fileMonitor.start();
		System.out.println("热部署模块正在监听目录: " + monitoredDir.getName().getPath());
		while(true) {
			Thread.sleep(1300);
		}
	}
}
